<?php
/* 
 * EachGo电商网站 新媒体
 * www.eachgo.cn
 */
namespace Home\Model;
use Think\Model;
class OrderInfoModel extends Model{    
    /*添加分享*/
    public function addOrder($data){
        return $this->add($data);
    }
    /*删除分享*/
    public function deleteOrder($id){
        $sql="UPDATE ".$this->tablePrefix."order_info SET isUserDelete=TRUE WHERE ID=".$id;
        //return $this->where("ID=".$id)->delete();
        return $this->execute($sql);
    }
    public function queryById($orderId){

    }
    public function queryAll(){
            $isBingo=$this->where("isUserDelete=".TRUE." AND isBingo=".TRUE )->order('orderDate desc')->select();
            $isBuy =$this->where("isUserDelete=".TRUE." AND isBingo= false" )->order('orderDate desc')->select();
            return array_merge($isBingo,$isBuy);
    }
    public function queryAllByUserId($UserId){
            $isBingo=$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo=".TRUE )->order('orderDate')->select();
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false" )->order('orderDate')->select();
            return array_merge($isBingo,$isBuy);
    }
    public function queryAllBingo($UserId,$condition){
        if($condition==0){//全部拼购订单
            $isBingo=$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo=".TRUE )->order('orderDate desc')->select();
            //$isBingo="SELECT * FROM EachGo.`t_order_info` WHERE isUserDelete=TRUE AND isBingo =TRUE AND user_ID=".$UserId." ORDER BY orderDate ";
        }else if($condition==1){//已开奖的拼购订单
            $isBingo=$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo=".TRUE." AND confirmReceiptDate>'1971-01-01'" )->order('orderDate desc')->select();
            //$isBingo="SELECT * FROM EachGo.`t_order_info` WHERE isUserDelete=TRUE AND isBingo =TRUE AND user_ID=".$UserId." AND confirmReceiptDate<'1971-01-01' ORDER BY orderDate";
        }else if($condition==2){//还未开奖的拼购订单
            $isBingo=$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo=".TRUE." AND confirmReceiptDate<'1970-01-01'"  )->order('orderDate desc')->select();
            // $isBingo="SELECT * FROM EachGo.`t_order_info` WHERE isUserDelete=TRUE AND isBingo =TRUE AND user_ID=".$UserId." AND confirmReceiptDate>'1971-01-01' ORDER BY orderDate";
        }
            //$reuslt=$this->query($isBingo);
            return $isBingo;
    }
    public function queryByDate($sql,$time){

    }
    public function queryAllBuy($UserId,$condition){
        if($condition==0){//全部订单
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false" )->order('orderDate desc')->select();
        }else if($condition==1){//待付款订单
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false AND payDate<'1971-01-01'" )->order('orderDate desc')->select();
        }else if($condition==2){//待发货订单
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false AND deliveryDate<'1971-01-01' AND payDate>'1971-01-01'" )->order('orderDate desc')->select();
        }else if($condition==3){//待收货订单
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false AND confirmReceiptDate<'1970-01-01' AND deliveryDate<'1971-01-01'" )->order('orderDate desc')->select();
        }else if($condition==4){//待评价订单
            $isBuy =$this->where("user_ID=".$UserId." AND isUserDelete= 0 "." AND isBingo= false AND confirmReceiptDate>'1970-01-01'" )->order('orderDate')->select();
        }
            return $isBuy;
    }
    public function update($data){
        return $this->where("ID=".$data['id'])->save($data);
    }
    public function getNumberBuy($userId){
        $sql="SELECT COUNT(*) AS pay FROM EachGo.`t_order_info` WHERE isBingo= FALSE
        AND payDate<'1971-01-01' AND EachGo.`t_order_info`.user_ID=".$userId;
        $pay=$this->query($sql);

        $sql="SELECT COUNT(*) AS send
        FROM EachGo.`t_order_info` WHERE isBingo= FALSE
        AND deliveryDate<'1971-01-01' AND payDate>'1971-01-01'
        AND EachGo.`t_order_info`.`user_ID`=2".$userId;
        $send=$this->query($sql);

        $sql="SELECT COUNT(*) AS receive
        FROM EachGo.`t_order_info` WHERE isBingo= FALSE
        AND confirmReceiptDate<'1970-01-01' AND deliveryDate<'1971-01-01'
        AND EachGo.`t_order_info`.`user_ID`=".$userId;
        $receive=$this->query($sql);

        $sql="SELECT COUNT(DISTINCT b.`orderInfo_ID`) AS evaluated
            FROM EachGo.`t_order_comment_info` AS a RIGHT JOIN EachGo.`t_order_detailed_info` AS b
            ON  a.`order_Detailed_Info_ID`=b.`ID`
            WHERE  a.`date` IS NULL AND b.`orderInfo_ID`
            IN(SELECT ID FROM EachGo.`t_order_info`
            WHERE EachGo.`t_order_info`.`user_ID`=".$userId."
            AND EachGo.`t_order_info`.`confirmReceiptDate`>'1971-01-01'
            AND EachGo.`t_order_info`.`isBingo`=0)";
        $evaluated=$this->query($sql);

        return array_merge($pay[0],$send[0],$receive[0],$evaluated[0]);
    }
    public function findSinleInfo($orderid){
        $sql="SELECT a.orderDate,b.consignee,b.address,b.zipCode,b. contactPhone,c.customerNote
            FROM EachGo.`t_order_info` a JOIN EachGo.`t_shipping_address` b
            ON a.shippingAddress_ID = b.`ID` JOIN EachGo.`t_order_detailed_info` c
            ON a.`ID` = c.orderInfo_ID
            WHERE a.`ID`=".$orderid;
        return $this->query($sql);
    }
}
